package model

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"strings"
)

// InitOutput 配置列表输出
type InitOutput struct {
	Source   map[string]*InitOutputSource   `json:"source"`
	TreeNode []*InitOutputTreeNode `json:"treeNode"`
}

// InitOutputSource 配置列表输出(原始)
type InitOutputSource struct {
	Label string `json:"label"`
	Value string `json:"value"`
	Pid   string `json:"pid"`
}

// InitOutputTreeNode 配置列表输出 (树)
type InitOutputTreeNode struct {
	Label    string       `json:"label"`
	Value    string       `json:"value"`
	Children []*InitOutputTreeNode `json:"children,omitempty"`
}

// PaginationInput 分页传入
type PaginationInput struct {
	Page     int `json:"page"`
	Start    int
	PageSize int    `json:"pageSize"`
	Search   string `json:"search"`
}

// PaginationOutPut 分页输出
type PaginationOutPut struct {
	Rows    interface{} `json:"rows"`
	Total   int64       `json:"total"`
	Summary string      `json:"summary,omitempty"`
}

// GetPaginationInput 获取分页 和搜索数据
func GetPaginationInput(ctx *gin.Context) (input *PaginationInput) {
	input = &PaginationInput{
		Page:     cast.ToInt(ctx.Query("page")),
		PageSize: cast.ToInt(ctx.Query("pageSize")),
		Search:   ctx.Query("search"),
	}
	input.Start = (input.Page - 1) * input.PageSize
	return
}

// GetQuerySearchInput 获取分页 和搜索数据
func GetQuerySearchInput(input *PaginationInput) (string, error) {
	if input.Search == "" {
		return "", nil
	}
	tmp := make(map[string]string)
	err := json.Unmarshal([]byte(input.Search), &tmp)
	if err != nil {
		return "", err
	}
	if len(tmp) == 0 {
		return "", nil
	}
	condition := ""
	for key, value := range tmp {
		conditionTmp, err := BuildCondition(key, value)
		if err != nil {
			return "", err
		}
		condition += conditionTmp
	}
	return strings.Trim(strings.Trim(strings.TrimSpace(condition), "AND"), "OR"), nil
}

/*
* {"user_name":"sfdsf|include","depart_id":"1,2|=","role_id":"1,2|include"}
*
 */

//BuildCondition 构建查询语句 只适合单表条件
func BuildCondition(key, value string) (string, error) {
	arr := strings.Split(value, "|")
	fmt.Printf("%+v~~~%+v\n", arr, value)
	if len(arr) != 2 {
		return "", errors.New("参数不正确")
	}

	/*<el-option label="等于" value="="></el-option>
	<el-option label="不等于" value="!="></el-option>
	<el-option label="大于" value=">"></el-option>
	<el-option label="大于等于" value=">="></el-option>
	<el-option label="小于" value="<"></el-option>
	<el-option label="小于等于" value="<="></el-option>
	<el-option label="包含" value="include"></el-option>
	<el-option label="不包含" value="notinclude"></el-option>*/

	condition := ""
	tmp := ""
	vars := strings.Split(arr[0], ",")
	fmt.Printf("vars:%+v\n", vars)

	switch arr[1] {
	case "=": //等于
		fmt.Printf("等于:%+v\n", vars)

		condition = fmt.Sprintf("(`%s` in ('%s')) AND ", key, strings.Join(vars, "','"))
	case "!=": //不等于
		fmt.Printf("不等于:%+v\n", vars)

		condition = fmt.Sprintf("(`%s` not in ('%s')) AND ", key, strings.Join(vars, "','"))
	case ">": //大于
		fmt.Printf("大于:%+v\n", vars)

		condition = "("
		for _, v := range vars {
			tmp += fmt.Sprintf("(`%s` > '%s') AND ", key, v)
		}
		condition += strings.Trim(tmp, "AND ") + ") AND"

	case ">=": //大于等于
		fmt.Printf("大于等于:%+v\n", vars)

		condition = "("
		for _, v := range vars {
			tmp += fmt.Sprintf("(`%s` >= '%s') AND ", key, v)
		}
		condition += strings.Trim(tmp, "AND ") + ") AND "
	case "<": //小于
		fmt.Printf("小于:%+v\n", vars)

		condition = "("
		for _, v := range vars {
			tmp += fmt.Sprintf("(`%s` < '%s') AND ", key, v)
		}
		condition += strings.Trim(tmp, "AND ") + ") AND "
	case "<=": //小于等于
		fmt.Printf("小于等于:%+v\n", vars)

		condition = "("
		for _, v := range vars {
			tmp += fmt.Sprintf("(`%s` <= '%s') AND ", key, v)
		}
		condition += strings.Trim(tmp, "AND ") + ") AND "

	case "include": //包含
		fmt.Printf("包含:%+v\n", vars)

		condition = "("
		for _, v := range vars {
			tmp += fmt.Sprintf("(`%s` like '%s%s%s') OR ", key, "%", v, "%")
		}
		condition += strings.Trim(tmp, "OR ") + ")  AND "

	case "notinclude": //不包含
		fmt.Printf("不包含:%+v\n", vars)

		condition = "("
		for _, v := range vars {
			tmp += fmt.Sprintf("(`%s` not like '%s%s%s') AND ", key, "%", v, "%")
		}
		condition += strings.Trim(tmp, "AND ") + ") AND "
	}
	fmt.Printf("condition:%+v\n", condition)

	return condition, nil
}
